package iface.aplicativo.negocio.entidade;

import iface.arquitetura.entidade.Entidade;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "menu")
@SequenceGenerator(name = "sq_menu", sequenceName = "sq_menu", allocationSize = 1, initialValue = 1)
public class Menu implements Entidade, Comparable<Menu> {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sq_menu")
	@Column(name = "id_menu")
	private Long idMenu;

	@Column(name = "nm_menu")
	private String nmMenu;

	@Column(name = "nm_pagina_inicial")
	private String nmPaginaInicial;

	@Column(name = "url_pasta")
	private String urlPasta;

	@Column(name = "nr_posicao")
	private Long nrPosicao;

	@Column(name = "id_menu_pai")
	private Long idMenuPai;

	@ManyToOne
	@JoinColumn(name = "id_menu_pai", referencedColumnName = "id_menu", insertable = false, updatable = false)
	private Menu menuPai;

	@OneToMany(mappedBy = "menuPai")
	@OrderBy("nrPosicao asc")
	private List<Menu> filhos;

	@ManyToMany(mappedBy = "menus")
	private List<Perfil> perfils;

	public Menu() {
	}

	public Menu(Long idMenu, String nmMenu) {
		this.idMenu = idMenu;
		this.nmMenu = nmMenu;
	}

	public Serializable getIdentificador() {
		return getIdMenu();
	}

	public Long getIdMenu() {
		return this.idMenu;
	}

	public void setIdMenu(Long idMenu) {
		this.idMenu = idMenu;
	}

	public String getNmMenu() {
		return this.nmMenu;
	}

	public void setNmMenu(String nmMenu) {
		this.nmMenu = nmMenu;
	}

	public String getNmPaginaInicial() {
		return this.nmPaginaInicial;
	}

	public void setNmPaginaInicial(String nmPaginaInicial) {
		this.nmPaginaInicial = nmPaginaInicial;
	}

	public String getUrlPasta() {
		return this.urlPasta;
	}

	public void setUrlPasta(String urlPasta) {
		this.urlPasta = urlPasta;
	}

	public List<Perfil> getPerfils() {
		return this.perfils;
	}

	public void setPerfils(List<Perfil> perfils) {
		this.perfils = perfils;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((idMenu == null) ? 0 : idMenu.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Menu))
			return false;
		Menu other = (Menu) obj;
		if (idMenu == null) {
			if (other.idMenu != null)
				return false;
		} else if (!idMenu.equals(other.idMenu))
			return false;
		return true;
	}

	public Menu getMenuPai() {
		return menuPai;
	}

	public void setMenuPai(Menu menuPai) {
		this.menuPai = menuPai;
	}

	public List<Menu> getFilhos() {
		return filhos;
	}

	public void setFilhos(List<Menu> filhos) {
		this.filhos = filhos;
	}

	public Long getNrPosicao() {
		return nrPosicao;
	}

	public void setNrPosicao(Long nrPosicao) {
		this.nrPosicao = nrPosicao;
	}

	public Long getIdMenuPai() {
		return idMenuPai;
	}

	public void setIdMenuPai(Long idMenuPai) {
		this.idMenuPai = idMenuPai;
	}

	public int compareTo(Menu arg0) {
		return this.getNrPosicao().compareTo(arg0.getNrPosicao());
	}

}